package com.realgodo.site.web.user.secret;

import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.realgodo.site.entity.application.secret.Secret;
import com.realgodo.site.entity.application.secret.SecretJoiner;
import com.realgodo.site.service.application.SecretJoinerManager;
import com.realgodo.site.service.application.SecretManager;
import com.realgodo.site.service.relation.FriendManager;
import com.realgodo.site.web.user.UserDefaultController;


@Results({
	@Result(name = "toindex", type = "redirect", location = "${base}/user/secret/"),
	@Result(name = "joined", type = "redirect", location = "${base}/user/secret/index!detail?id=${id}"),
	@Result(name = "commentary", type = "redirect", location = "${base}/user/secret/index!mySecret")
})
public class IndexController extends UserDefaultController{

	/***/
	private static final long serialVersionUID = 1L;
	@Autowired
	private SecretManager secretManager;
	@Autowired
	private SecretJoinerManager secretJoinerManager;
	@Autowired
	private FriendManager friendsManager;
	/**秘密实体*/
	private Secret secret;
	
	private List<SecretJoiner> secretJoinerList;
	
	private String answer;
	/** 秘密分页信息 */
	private Page<Secret> secretPage;
	@Override
	public String execute() throws Exception {
		return pub();
	}
	
	/**发其秘密页*/
	public String pub(){
		return SUCCESS;
	}
	
	/**热门秘密页*/
	public String hots(){
		super.getParamMap().put("hotlimit", HOT_LIMIT);
		super.getParamMap().put("spaceId", super.getSpace().getId().toString());
		super.getParamMap().put("spaceIds", friendsManager.getFriendSpaceIdList(super.getSpace().getId()));
		this.list();
		return "hots";
	}
	
	/**参与秘密*/
	public String join(){
		if(secretManager.isHaveJoin(super.getSpace().getId(), id)){
			return "joined";
		}
		this.secret=secretManager.get(id);
		return "tojoin";
	}
	
	/**保存参与者*/
	public String saveJoiner(){
		secret=secretManager.get(id);
		secret.setJoiners(secret.getJoiners()+1);
		secretManager.save(secret);
		SecretJoiner secretJoiner=new SecretJoiner();
		secretJoiner.setAnswer(answer);
		secretJoiner.setSecret(secret);
		secretJoiner.setSpace(super.getSpace());
		secretJoinerManager.save(secretJoiner);
		return "joined";
	}
	
	/**热点评论详细*/
	public String detail(){
		this.secret=secretManager.get(id);
		secretJoinerList=secretJoinerManager.getSecretJoinerList(id, 0);
		return "detail";
	}
	
	/**我的秘密页*/
	public String mySecret(){
		super.getParamMap().put("spaceId", super.getSpace().getId().toString());
		this.list();
		return "my_secret";
	}
	
	/**保存秘密*/
	public String save(){
		secret.setSpace(super.getSpace());
		secret.setPubTime(new Date());
		secret.setClasz(Secret.class);
		this.secretManager.save(secret);
		return "commentary";
	}
	
	public String list(){
		this.secretPage=secretManager.find(PAGE_SIZE, super.getParamMap());
		super.setPageInfo(secretPage);
//		super.pageInfo=secretPage;
		return "my_secret";
	}

	
	
	//-------get/set--------//
	public Secret getSecret() {
		return secret;
	}

	public void setSecret(Secret secret) {
		this.secret = secret;
	}

	public Page<Secret> getSecretPage() {
		return secretPage;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public List<SecretJoiner> getSecretJoinerList() {
		return secretJoinerList;
	}

	
	
	
}
